import React, { useState } from "react";
import { withRouter } from "react-router-dom";
import { Layout, Input, Form, Button, Divider, message } from "antd";
import { UserOutlined, LockOutlined } from "@ant-design/icons";
import store from "../../store";
import { USER_INFO, USER_TOKEN, USER_AUTH } from "../../store/actionTypes";
// import axios from '@/api'
// import { API } from '@/api/config'
import "@/style/view-style/login.less";

const Login = props => {
  const [loading, setLoading] = useState(false);

  const handleSubmitFinish = values => {
    var shuju = [{ "title": "首页", "icon": "DashboardFilled", "path": "/", "isShow": true, "id": "M000001", "parentId": "0", "child": [] }, { "title": "产品", "icon": "AppstoreOutlined", "path": "/product", "isShow": true, "id": "M000002", "parentId": "0", "child": [{ "title": "缺货清单", "icon": "ContainerOutlined", "path": "/bo/list", "isShow": true, "id": "M000003", "parentId": "M000002", "child": [] }, { "title": "产品信息", "icon": "AppstoreOutlined", "path": "/product/list", "isShow": true, "id": "M000100", "parentId": "M000002", "child": [] }] }, { "title": "订单", "icon": "ShoppingOutlined", "path": "/order", "isShow": true, "id": "M000018", "parentId": "0", "child": [{ "title": "询价", "icon": "MessageOutlined", "path": "/quote/list", "isShow": true, "id": "M000111", "parentId": "M000018", "child": [] }, { "title": "在线订单", "icon": "ShoppingOutlined", "path": "/shopOrder/list", "isShow": true, "id": "M000019", "parentId": "M000018", "child": [] }, { "title": "订单列表", "icon": "ShoppingOutlined", "path": "/order/list", "isShow": true, "id": "M000122", "parentId": "M000018", "child": [] }, { "title": "订单明细", "icon": "ShoppingOutlined", "path": "/orderDetail/list", "isShow": true, "id": "M000145", "parentId": "M000018", "child": [] }, { "title": "发货明细", "icon": "ShoppingOutlined", "path": "/shipDetail/list", "isShow": true, "id": "M000149", "parentId": "M000018", "child": [] }, { "title": "发货管理", "icon": "NodeIndexOutlined", "path": "/ship", "isShow": true, "id": "M000152", "parentId": "M000018", "child": [{ "title": "免费样审核", "icon": null, "path": "/order/freeSample", "isShow": true, "id": "M000157", "parentId": "M000152", "child": [] }, { "title": "待发货", "icon": null, "path": "/waitingShip/list", "isShow": true, "id": "M000156", "parentId": "M000152", "child": [] }, { "title": "期货", "icon": null, "path": "/backOrder/list", "isShow": true, "id": "M000161", "parentId": "M000152", "child": [] }, { "title": "核对发货", "icon": null, "path": "/confirmShip/list", "isShow": true, "id": "M000167", "parentId": "M000152", "child": [] }, { "title": "发货记录", "icon": null, "path": "/ship/list", "isShow": true, "id": "M000179", "parentId": "M000152", "child": [] }] }, { "title": "服务", "icon": "CustomerServiceOutlined", "path": "/customAntigen", "isShow": true, "id": "M000187", "parentId": "M000018", "child": [{ "title": "代加工", "icon": null, "path": "/customAntigen/list", "isShow": true, "id": "M000188", "parentId": "M000187", "child": [] }, { "title": "生成代检测", "icon": null, "path": "/customTesting/waitingTestList", "isShow": true, "id": "M000343", "parentId": "M000187", "child": [] }, { "title": "代检测", "icon": null, "path": "/customTesting/list", "isShow": true, "id": "M000346", "parentId": "M000187", "child": [] }] }] }, { "title": "财务", "icon": "MoneyCollectOutlined", "path": "/bill", "isShow": true, "id": "M000201", "parentId": "0", "child": [{ "title": "现结代开账单", "icon": "ImportOutlined", "path": "/waitingBill/list", "isShow": true, "id": "M000202", "parentId": "M000201", "child": [] }, { "title": "账单列表", "icon": "AccountBookOutlined", "path": "/bill/list", "isShow": true, "id": "M000208", "parentId": "M000201", "child": [] }, { "title": "付款列表", "icon": "TransactionOutlined", "path": "/payment/list", "isShow": true, "id": "M000224", "parentId": "M000201", "child": [] }, { "title": "发票", "icon": "GroupOutlined", "path": "/invoice/list", "isShow": true, "id": "M000227", "parentId": "M000201", "child": [] }, { "title": "用户存款", "icon": "WalletOutlined", "path": "/creditMemo/list", "isShow": true, "id": "M000230", "parentId": "M000201", "child": [] }, { "title": "统计账单", "icon": "PieChartOutlined", "path": "/stiatic/billStiatic", "isShow": true, "id": "M000342", "parentId": "M000201", "child": [] }] }, { "title": "代理商", "icon": "TeamOutlined", "path": "/partners", "isShow": true, "id": "M000026", "parentId": "0", "child": [{ "title": "代理商信息", "icon": "ShoppingOutlined", "path": "/partners/list", "isShow": true, "id": "M000027", "parentId": "M000026", "child": [] }, { "title": "AM代理商", "icon": "ShoppingOutlined", "path": "/partners/amPartnerList", "isShow": true, "id": "M000540", "parentId": "M000026", "child": [] }, { "title": "代理商销售情况", "icon": "ShoppingOutlined", "path": "/partnerSale/list", "isShow": true, "id": "M000237", "parentId": "M000026", "child": [] }, { "title": "代理商任务完成情况", "icon": "ShoppingOutlined", "path": "/partnerSale/detailList", "isShow": true, "id": "M000286", "parentId": "M000026", "child": [] }, { "title": "内部任务完成情况", "icon": "ShoppingOutlined", "path": "/employeeSale/list", "isShow": true, "id": "M000240", "parentId": "M000026", "child": [] }] }, { "title": "用户", "icon": "TeamOutlined", "path": "/customer", "isShow": true, "id": "M000051", "parentId": "0", "child": [{ "title": "客户信息", "icon": "UserOutlined", "path": "/customer/list", "isShow": true, "id": "M000052", "parentId": "M000051", "child": [] }, { "title": "机构信息", "icon": "ApartmentOutlined", "path": "/institution/list", "isShow": true, "id": "M000064", "parentId": "M000051", "child": [] }, { "title": "终端用户信息", "icon": "ContainerOutlined", "path": "/leads/list", "isShow": true, "id": "M000071", "parentId": "M000051", "child": [] }, { "title": "待处理投诉", "icon": "AliwangwangOutlined", "path": "/complain/waitProcessList", "isShow": true, "id": "M000378", "parentId": "M000051", "child": [] }, { "title": "驳回投诉", "icon": "AliwangwangOutlined", "path": "/complain/notPassList", "isShow": true, "id": "M000533", "parentId": "M000051", "child": [] }, { "title": "售后与投诉", "icon": "AliwangwangOutlined", "path": "/complain/list", "isShow": true, "id": "M000242", "parentId": "M000051", "child": [] }, { "title": "换货列表", "icon": "AliwangwangOutlined", "path": "/complain/reshipList", "isShow": true, "id": "M000297", "parentId": "M000051", "child": [] }, { "title": "退款列表", "icon": "AliwangwangOutlined", "path": "/complain/refundList", "isShow": true, "id": "M000298", "parentId": "M000051", "child": [] }, { "title": "免费样反馈", "icon": "ContainerOutlined", "path": "/feedback/list", "isShow": true, "id": "M000261", "parentId": "M000051", "child": [] }] }, { "title": "市场", "icon": "TeamOutlined", "path": "/market", "isShow": true, "id": "M000078", "parentId": "0", "child": [{ "title": "文献回馈", "icon": "BookOutlined", "path": "/market/citationReward/list", "isShow": true, "id": "M000079", "parentId": "M000078", "child": [] }] }, { "title": "设置", "icon": "SettingOutlined", "path": "/employee", "isShow": true, "id": "M000087", "parentId": "0", "child": [{ "title": "员工列表", "icon": "UserOutlined", "path": "/employee/list", "isShow": true, "id": "M000088", "parentId": "M000087", "child": [] }, { "title": "菜单管理", "icon": "MenuOutlined", "path": "/menuList/list", "isShow": true, "id": "M000265", "parentId": "M000087", "child": [] }, { "title": "角色管理", "icon": "MenuOutlined", "path": "/role/list", "isShow": true, "id": "M000275", "parentId": "M000087", "child": [] }] }, { "title": "统计", "icon": "AreaChartOutlined", "path": "/stiatic", "isShow": true, "id": "M000271", "parentId": "0", "child": [{ "title": "销售种类统计", "icon": "PieChartOutlined", "path": "/stiatic/saleStiatic", "isShow": true, "id": "M000272", "parentId": "M000271", "child": [] }, { "title": "产品分类统计", "icon": "PieChartOutlined", "path": "/stiatic/productDetailStiatic", "isShow": true, "id": "M000299", "parentId": "M000271", "child": [] }] }, { "title": "积分商城", "icon": "AreaChartOutlined", "path": "/mall", "isShow": true, "id": "M000300", "parentId": "0", "child": [{ "title": "客户管理", "icon": "UserOutlined", "path": "/mall/mallCustomer/list", "isShow": true, "id": "M000301", "parentId": "M000300", "child": [] }, { "title": "积分日志", "icon": "PieChartOutlined", "path": "/mall/pointLog/list", "isShow": true, "id": "M000305", "parentId": "M000300", "child": [] }, { "title": "供应商管理", "icon": "PieChartOutlined", "path": "/mall/vendor/list", "isShow": true, "id": "M000307", "parentId": "M000300", "child": [] }, { "title": "产品管理", "icon": "PieChartOutlined", "path": "/mall/mallProduct/list", "isShow": true, "id": "M000312", "parentId": "M000300", "child": [] }, { "title": "采购管理", "icon": "PieChartOutlined", "path": "/mall/po/list", "isShow": true, "id": "M000317", "parentId": "M000300", "child": [] }, { "title": "礼品分发", "icon": "PieChartOutlined", "path": "/mall/export/list", "isShow": true, "id": "M000326", "parentId": "M000300", "child": [] }, { "title": "兑换列表", "icon": "PieChartOutlined", "path": "/mall/redeem/list", "isShow": true, "id": "M000332", "parentId": "M000300", "child": [] }, { "title": "问卷列表", "icon": "PieChartOutlined", "path": "/mall/questionnaire/list", "isShow": true, "id": "M000551", "parentId": "M000300", "child": [] }] }, { "title": "网站相关", "icon": "AreaChartOutlined", "path": "/website", "isShow": true, "id": "M000535", "parentId": "0", "child": [{ "title": "宣传册", "icon": "AreaChartOutlined", "path": "/website/flyer/list", "isShow": true, "id": "M000536", "parentId": "M000535", "child": [] }] }, { "title": "个人中心", "icon": "TeamOutlined", "path": "/center", "isShow": true, "id": "M000541", "parentId": "0", "child": [{ "title": "代理商任务完成情况", "icon": "ShoppingOutlined", "path": "/partnerSale/myDetailList", "isShow": true, "id": "M000542", "parentId": "M000541", "child": [] }, { "title": "内部任务完成情况", "icon": "ShoppingOutlined", "path": "/employeeSale/myList", "isShow": true, "id": "M000543", "parentId": "M000541", "child": [] }, { "title": "代理商销售情况", "icon": "ShoppingOutlined", "path": "/partnerSale/myList", "isShow": true, "id": "M000544", "parentId": "M000541", "child": [] }, { "title": "销售种类统计", "icon": "PieChartOutlined", "path": "/stiatic/saleMyStiatic", "isShow": true, "id": "M000550", "parentId": "M000541", "child": [] }] }];
    console.log("Success:", values);
    // let { username, password } = values
    // axios
    //     .post(`${API}/login`, { username, password })
    //     .then(res => {
    //         if (res.data.code === 0) {
    //             localStorage.setItem('user', JSON.stringify(res.data.data.user))
    //             localStorage.setItem('token', res.data.data.token)
    //             props.history.push('/')
    //             message.success('登录成功!')
    //         } else {
    //             message.error('登录失败!')
    //         }
    //     })
    //     .catch(err => {
    //         message.error('登录失败!')
    //     })

    // 这里可以做权限校验 模拟接口返回用户权限标识
    switch (values.username) {
      case "admin":
        values.auth = 0;
        break;
      default:
        values.auth = 1;
    }
    //模拟数据
    const actionInfo = {
      type: USER_INFO,
      value: { name: "wyg", role: "admin", depart: "IT" }
    };
    const actionToken = {
      type: USER_TOKEN,
      value: "aoisjdoajwdjaowdnaowdaomwdmaszazaaaz"
    };
    const actionAuth = {
      type: USER_AUTH,
      value: shuju
    };
    localStorage.setItem("USER_TOKEN", JSON.stringify("aoisjdoajwdjaowdnaowdaomwdmaszazaaaz"));
    store.dispatch(actionInfo);
    store.dispatch(actionToken);
    store.dispatch(actionAuth);
    setLoading(true);
    setTimeout(() => {
      message.success("登录成功!");
      props.history.push("/");
    }, 2000);
  };

  const handleSubmitFinishFailed = errorInfo => {
    console.log("Failed:", errorInfo);
  };

  // useEffect(() => {
  //   notification.open({
  //     message: "欢迎使用后台管理平台",
  //     duration: null,
  //     description: "账号 admin(管理员) 其他(游客) 密码随意"
  //   });
  //   return () => {
  //     notification.destroy();
  //   };
  // }, []);

  return (
    <Layout className="login animated fadeIn">
      <div className="model">
        <div className="login-form">
          <h3>后台管理系统</h3>
          <Divider />
          <Form onFinish={handleSubmitFinish} onFinishFailed={handleSubmitFinishFailed}>
            <Form.Item
              // label="Username"
              name="username"
              rules={[{ required: true, message: "请输入用户名" }]}
            >
              <Input placeholder="用户名" prefix={<UserOutlined className="site-form-item-icon" />} />
            </Form.Item>
            <Form.Item
              // label="Password"
              name="password"
              rules={[{ required: true, message: "请输入密码" }]}
            >
              <Input.Password
                type="password"
                placeholder="密码"
                prefix={<LockOutlined className="site-form-item-icon" />}
              />
            </Form.Item>
            <Form.Item>
              <Button type="primary" htmlType="submit" className="login-form-button" loading={loading}>
                登录
              </Button>
            </Form.Item>
          </Form>
        </div>
      </div>
    </Layout>
  );
};

export default withRouter(Login);
